package bb.lanxing.lib.devices.core.sync;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import bb.lanxing.util.Log;

public class SyncStatusNotifier {
    private static final int MESG_DEVICE_STATUS = 4;
    private static final int MESG_GET_FILES = 2;
    private static final int MESG_SYNC_STATUS = 1;
    private static final int MESG_UPDATE_PROGRESS = 3;
    private Set<DeviceSyncListener> mListenerSet;
    private SyncHandlerOnMainThread mMainHandler = new SyncHandlerOnMainThread(this);

    public void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
        Set<DeviceSyncListener> set = this.mListenerSet;
        if (set != null) {
            set.remove(deviceSyncListener);
        }
    }

    public void registerSyncListener(DeviceSyncListener deviceSyncListener) {
        if (this.mListenerSet == null) {
            this.mListenerSet = new HashSet();
        }
        this.mListenerSet.add(deviceSyncListener);
    }

    public void notifySyncStatus(DeviceFile deviceFile, int i, String str) {
        if (this.mMainHandler == null) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("mesg", str);
        Message obtainMessage = this.mMainHandler.obtainMessage(1, i, 0, deviceFile);
        obtainMessage.setData(bundle);
        obtainMessage.sendToTarget();
    }

    public void notifySyncStatus(DeviceFile deviceFile, int i) {
        notifySyncStatus(deviceFile, i, null);
    }

    public void notifyProgressUpdate(DeviceFile deviceFile, float f) {
        if (this.mMainHandler == null) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putFloat("progress", f);
        Message obtainMessage = this.mMainHandler.obtainMessage(3, deviceFile);
        obtainMessage.setData(bundle);
        this.mMainHandler.sendMessage(obtainMessage);
    }

    public void notifyReadFileList(List<DeviceFile> list) {
        SyncHandlerOnMainThread syncHandlerOnMainThread = this.mMainHandler;
        if (syncHandlerOnMainThread == null) {
            return;
        }
        syncHandlerOnMainThread.obtainMessage(2, list).sendToTarget();
    }

    public void notifyDeviceStatus(byte b) {
        SyncHandlerOnMainThread syncHandlerOnMainThread = this.mMainHandler;
        if (syncHandlerOnMainThread == null) {
            return;
        }
        syncHandlerOnMainThread.obtainMessage(4, b).sendToTarget();
    }

    public void dispatchStatus(DeviceFile deviceFile, int i, String str) {
        for (DeviceSyncListener deviceSyncListener : this.mListenerSet) {
            deviceSyncListener.onSyncStatus(deviceFile, i, str);
        }
    }

    public void dispatchFiles(List<DeviceFile> list) {
        for (DeviceSyncListener deviceSyncListener : this.mListenerSet) {
            deviceSyncListener.onReadFile(list != null ? new ArrayList(list) : null);
        }
    }

    public void dispatchProgress(DeviceFile deviceFile, float f) {
        for (DeviceSyncListener deviceSyncListener : this.mListenerSet) {
            deviceSyncListener.onProgressUpdate(deviceFile, f);
        }
    }

    public void dispatchDeviceStatus(byte b) {
        for (DeviceSyncListener deviceSyncListener : this.mListenerSet) {
            deviceSyncListener.onDeviceStatus(b);
        }
    }

    public void release() {
        Set<DeviceSyncListener> set = this.mListenerSet;
        if (set != null) {
            set.clear();
        }
        SyncHandlerOnMainThread syncHandlerOnMainThread = this.mMainHandler;
        if (syncHandlerOnMainThread != null) {
            syncHandlerOnMainThread.removeCallbacksAndMessages(null);
            this.mMainHandler.nofifier = null;
            this.mMainHandler = null;
        }
    }


    public static class SyncHandlerOnMainThread extends Handler {
        SyncStatusNotifier nofifier;

        SyncHandlerOnMainThread(SyncStatusNotifier syncStatusNotifier) {
            super(Looper.getMainLooper());
            this.nofifier = syncStatusNotifier;
        }

        @Override
        public void handleMessage(Message message) {
            SyncStatusNotifier syncStatusNotifier = this.nofifier;
            if (syncStatusNotifier == null || syncStatusNotifier.mListenerSet == null || this.nofifier.mListenerSet.isEmpty()) {
                return;
            }
            Bundle data = message.getData();
            int i = message.what;
            Log.d("wangsm_bt", "SyncStatusNotifier, handlemessage, what: " + i);
            if (i == 1) {
                this.nofifier.dispatchStatus((DeviceFile) message.obj, message.arg1, data != null ? data.getString("mesg") : null);
            } else if (i == 2) {
                this.nofifier.dispatchFiles((List) message.obj);
            } else if (i == 3) {
                this.nofifier.dispatchProgress((DeviceFile) message.obj, data != null ? data.getFloat("progress") : 0.0f);
            } else if (i == 4) {
                this.nofifier.dispatchDeviceStatus((Byte) message.obj);
            }
        }
    }
}
